package com.lc.auth.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lc.auth.service.SysRoleService;
import com.lc.common.result.Result;
import com.lc.model.system.SysRole;
import com.lc.vo.system.AssginRoleVo;
import com.lc.vo.system.SysRoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @GetMapping("findAll")
    public List<SysRole> findAll() {
        return sysRoleService.list();
    }

    @ApiOperation(value = "条件分页查询")
    @GetMapping("{page}/{limit}")
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    public Result pageQueryRole(@PathVariable Long page, @PathVariable Long limit, SysRoleQueryVo sysRoleQueryVo){
        Page<SysRole> pageModel = sysRoleService.pageQueryRole(page, limit, sysRoleQueryVo);
        return Result.ok(pageModel);
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @ApiOperation(value = "新增角色")
    @PostMapping("addRole")
    public Result addRole(@RequestBody SysRole sysRole) {
        boolean b = sysRoleService.save(sysRole);
        return b ? Result.ok() : Result.fail();
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation(value = "根据id查询角色")
    @GetMapping("getRole/{id}")
    public Result getRole(@PathVariable Long id){
        SysRole sysRole = sysRoleService.getById(id);
        return Result.ok(sysRole);
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation(value = "修改角色")
    @PutMapping("updateRole")
    public Result updateRole(@RequestBody SysRole sysRole){
        boolean b = sysRoleService.updateById(sysRole);
        return b ? Result.ok() : Result.fail();
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation(value = "删除角色")
    @DeleteMapping("remove/{id}")
    public Result deleteRole(@PathVariable Long id){
        boolean b = sysRoleService.removeById(id);
        return b ? Result.ok() : Result.fail();
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation(value = "批量删除角色")
    @DeleteMapping("deleteByBatch")
    public Result deleteByBatch(@RequestBody List<Long> ids){
        boolean b = sysRoleService.removeByIds(ids);
        return b ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable Long userId) {
        Map<String, Object> roleMap = sysRoleService.findRoleByAdminId(userId);
        return Result.ok(roleMap);
    }

    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo) {
        sysRoleService.doAssign(assginRoleVo);
        return Result.ok();
    }


}
